// SecondPGApp.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "SecondPGApp.h"
#include"CTest.h"

#define MAX_LOADSTRING 100

int IEventListener::numInstances;


// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);



int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	g_PG_pSGameInfo->SetInfo(1024, 768, true, true, L"SecondTest", NULL);
	MyRegisterClass(hInstance);
	ShowWindow(g_PG_pSGameInfo->GetWindowHandle(), nCmdShow);
	
	g_PG_pPaleGemFramework->pgInit(hInst, nCmdShow, g_PG_pSGameInfo->GetGameTitle());

	g_PG_pInputEventManager->MapKey(VK_W, forward);
	g_PG_pInputEventManager->MapKey(VK_A, left);
	g_PG_pInputEventManager->MapKey(VK_S, backward);
	g_PG_pInputEventManager->MapKey(VK_D, right);
	g_PG_pInputEventManager->MapKey(VK_LBUTTON, leftMouse);
	g_PG_pInputEventManager->MapKey(VK_RBUTTON, rightMouse);


	
	CTest test;
	//g_pEventManager->RegisterListener(pTest);

	g_PG_pPaleGemFramework->pgDoMessageLoop();
	//g_pEventManager->UnregisterListener(pTest);

	g_PG_pPaleGemFramework->pgExit();
	g_PG_pPaleGemFramework->Del();

	return 0;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	// Create out a windowclass
	WNDCLASSEX windowClassEx;
	windowClassEx.cbSize = sizeof(WNDCLASSEX);
	windowClassEx.style = CS_HREDRAW | CS_VREDRAW;
    windowClassEx.lpfnWndProc = WndProc;
    windowClassEx.cbClsExtra = 0;
    windowClassEx.cbWndExtra = 0;
    windowClassEx.hInstance = hInstance;
    windowClassEx.hIcon = NULL;
    windowClassEx.hCursor = LoadCursor( NULL, IDC_ARROW );
    windowClassEx.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
    windowClassEx.lpszMenuName = NULL;
	windowClassEx.lpszClassName = L"GameApp window";
    windowClassEx.hIconSm = NULL;

	HRESULT hrCreationSuccess;
	if (FAILED(hrCreationSuccess = RegisterClassEx(&windowClassEx)))
	{
		return hrCreationSuccess;
	}


	// Create the actual window

	RECT windowRect = { 0, 0, g_PG_pSGameInfo->GetScreenWidth(), g_PG_pSGameInfo->GetScreenHeight()};
	AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);
	HWND window = CreateWindow(L"GameApp window" , L"GameApp window",
									WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,		//Style, x, 
									windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, //width, height
									NULL, NULL, hInstance, NULL); //parentWindow, Menu, Instance, lParam

	g_PG_pSGameInfo->SetWindowHandle(window);
	return S_OK;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
    HDC hdc;
	switch(message)
	{
	case WM_PAINT:
		{
            hdc = BeginPaint(hWnd, &ps);
            EndPaint(hWnd, &ps);
		}break;

    case WM_DESTROY:
		{
            PostQuitMessage(0);
		}break;

	case WM_KEYDOWN:
		{
			g_PG_pInputEventManager->RegisterEvent(wParam, eKPEC_press, NULL);
		}break;
	case WM_KEYUP:
		  {
			  g_PG_pInputEventManager->RegisterEvent(wParam, eKPEC_release, NULL);
		  }break;
	case WM_MOUSEMOVE:
		{
			g_PG_pMouseInputManager->RegisterEvent(lParam, eME_Move, NULL);
		}break;
	case WM_LBUTTONDOWN:
		{
			g_PG_pMouseInputManager->RegisterEvent(VK_LBUTTON, eME_ButtonClicked, NULL);
		}break;
	case WM_LBUTTONUP:
		{
			g_PG_pMouseInputManager->RegisterEvent(VK_LBUTTON, eME_ButtonReleased, NULL);
		}break;
		case WM_RBUTTONDOWN:
		{
			g_PG_pMouseInputManager->RegisterEvent(VK_RBUTTON, eME_ButtonClicked, NULL);
		}break;
	case WM_RBUTTONUP:
		{
			g_PG_pMouseInputManager->RegisterEvent(VK_RBUTTON, eME_ButtonReleased, NULL);
		}break;
		case WM_MBUTTONDOWN:
		{
			g_PG_pMouseInputManager->RegisterEvent(VK_MBUTTON, eME_ButtonClicked, NULL);
		}break;
	case WM_MBUTTONUP:
		{
			g_PG_pMouseInputManager->RegisterEvent(VK_MBUTTON, eME_ButtonReleased, NULL);
		}break;
	case WM_XBUTTONDOWN:
		{
			if (HIWORD(wParam) == 1)
				g_PG_pMouseInputManager->RegisterEvent(VK_XBUTTON1, eME_ButtonClicked, NULL);
			else
				g_PG_pMouseInputManager->RegisterEvent(VK_XBUTTON2, eME_ButtonClicked, NULL);
		};
	case WM_XBUTTONUP:
		{
			if (HIWORD(wParam) == 1)
				g_PG_pMouseInputManager->RegisterEvent(VK_XBUTTON1, eME_ButtonReleased, NULL);
			else
				g_PG_pMouseInputManager->RegisterEvent(VK_XBUTTON2, eME_ButtonReleased, NULL);
		}break;
	case WM_MOUSEWHEEL:
		{
			g_PG_pMouseInputManager->RegisterEvent(wParam, eME_WheelV, NULL);
		}break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}

	return 0;
}

